////////////////////////////////////////////////////////////////////////////////
//                                                                            //
//  Filename:    HydroHeatGuts.cpp											  //
//  Developer:																  //	
//  Create date:                                                              //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////
//                                                                            //
//  Description: This is the class implementation for the slot gut of the     //
//               Australian Slot machine called Hydro Heat.			          //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////
//        2010 - Riplife Gaming.  All rights reserved.						  //
////////////////////////////////////////////////////////////////////////////////

#include "ReelBloodGuts.h"
#include "slotconn.h"
#include "sltsql.h"

// Minimum bet for ReelBlood: 25 cents. All payouts in the database are calculated with a 25 cents bet
#define minimumBetRB 25

// 0: collect and end
// -1: win all and continue
static int32 g_poolPayouts[5][15] =
{
	// Pool 1 (BAR 1)
	{
		300,
		250,
		200,
		200,
		150,
		125,
		100,
		75,
		60,
		50,
		40,
		30,
		-1,
		0,
		0
	},
	// Pool 2 (BAR 1)
	{
		250,
		200,
		200,
		150,
		100,
		75,
		50,
		40,
		20,
		10,
		10,
		10,
		10,
		-1,
		0
	},
	// Pool 3 (BAR 1)
	{
		200,
		200,
		150,
		150,
		100,
		90,
		75,
		75,
		75,
		50,
		50,
		40,
		30,
		20,
		0
	},	
	// Pool 4 (BAR 1)
	{
		200,
		150,
		100,
		100,
		80,
		80,
		80,
		50,
		40,
		10,
		10,
		-1,
		-1,
		0,
		0
	},
	// Pool 5 (BAR 2)
	{
		300,
		275,
		200,
		200,
		150,
		125,
		100,
		80,
		75,
		50,
		40,
		0,
		0,
		0,
		0
	}
};


////////////////////////////////////////////////////////////////////////////
/// ReelBloodGuts::m_aScatterWinSpinResults
///
/// This array is used to quickly get the SpinResult for the number of
/// icons.  It is indexed by wheel icon and then count.  So to get the
/// SpinResult for 5 Scatter, use the following code snippet:
///
///		SpinResult result = m_aLineWinSpinResults[ ICON_SCATTER ][ 5 ];
///
/// In this example result would be set to EVT_FIVE_SCATTER.
////////////////////////////////////////////////////////////////////////////
ReelBloodGuts::SpinResult ReelBloodGuts::m_aScatterWinSpinResults[][6] = 
{
// Number of icons:			0			1				2						3					4					5
/* ICON_WILD */			{	EVT_LOSER,	EVT_LOSER,		EVT_LOSER,				EVT_LOSER,			EVT_LOSER,			EVT_LOSER			},
/* ICON_BOTTLE */		{	EVT_LOSER,	EVT_LOSER,		EVT_LOSER,				EVT_THREE_BOTTLE,	EVT_FOUR_BOTTLE,	EVT_FIVE_BOTTLE		},
/* ICON_HUNTER */		{	EVT_LOSER,	EVT_LOSER,		EVT_LOSER,				EVT_THREE_HUNTER,	EVT_FOUR_HUNTER,	EVT_FIVE_HUNTER		},
/* ICON_VAMPIRE */		{	EVT_LOSER,	EVT_LOSER,		EVT_LOSER,				EVT_THREE_VAMPIRE,	EVT_FOUR_VAMPIRE,	EVT_FIVE_VAMPIRE	},
/* ICON_VICTIM */		{	EVT_LOSER,	EVT_LOSER,		EVT_LOSER,				EVT_THREE_VICTIM,	EVT_FOUR_VICTIM,	EVT_FIVE_VICTIM		},
/* ICON_ACE */			{	EVT_LOSER,	EVT_LOSER,		EVT_LOSER,				EVT_THREE_ACE,		EVT_FOUR_ACE,		EVT_FIVE_ACE		},
/* ICON_KING */			{	EVT_LOSER,	EVT_LOSER,		EVT_LOSER,				EVT_THREE_KING,		EVT_FOUR_KING,		EVT_FIVE_KING		},
/* ICON_QUEEN */		{	EVT_LOSER,	EVT_LOSER,		EVT_LOSER,				EVT_THREE_QUEEN,	EVT_FOUR_QUEEN,		EVT_FIVE_QUEEN		},
/* ICON_JACK */			{	EVT_LOSER,	EVT_LOSER,		EVT_LOSER,				EVT_THREE_JACK,		EVT_FOUR_JACK,		EVT_FIVE_JACK		},
/* ICON_TEN */			{	EVT_LOSER,	EVT_LOSER,		EVT_LOSER,				EVT_THREE_TEN,		EVT_FOUR_TEN,		EVT_FIVE_TEN		},
/* ICON_NINE */			{	EVT_LOSER,	EVT_LOSER,		EVT_LOSER,				EVT_THREE_NINE,		EVT_FOUR_NINE,		EVT_FIVE_NINE		},
/* ICON_SCATTER */		{	EVT_LOSER,	EVT_LOSER,		EVT_SCATTER_NEAR_MISS,	EVT_THREE_SCATTER,	EVT_FOUR_SCATTER,	EVT_FIVE_SCATTER	}
};


////////////////////////////////////////////////////////////////////////////
/// @fn 	 ReelBloodGuts::ReelBloodGuts()   
///
/// This is the default constructor for the class.
////////////////////////////////////////////////////////////////////////////
ReelBloodGuts::ReelBloodGuts()
{
	// If this doesn't compile it means that someone probably added
	// another icon type but did not update the
	// m_aScatterWinSpinResults array. 
	C_ASSERT( countof( m_aScatterWinSpinResults ) == NUM_VALID_ICONS );
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 ReelBloodGuts::~ReelBloodGuts()
///
/// Destructor for ReelBloodGuts. 
////////////////////////////////////////////////////////////////////////////
ReelBloodGuts::~ReelBloodGuts()
{
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 ReelBloodGuts::classifyResult()
///
/// classifyResult for HydroHeatGuts. Redefined because this is an all pays
/// slot, we can't use the general one
////////////////////////////////////////////////////////////////////////////
void ReelBloodGuts::classifyResult(SlotSpin *spin)
{
    // Classify any scatter pays (implemented as a null function in the base)
	spin->clearScatters();
	
	classifyAllPays(spin);
	classifyScatters(spin);

    // Generate the wheel view for reporting
    storeWheelView(spin);
}

////////////////////////////////////////////////////////////////////////////
/// @fn		ReelBloodGuts::tryForJackpot()
///
///	This function tries to win the jackpot and adds the scatter if we hit
/// the jackpot.
////////////////////////////////////////////////////////////////////////////
void ReelBloodGuts::tryForJackpot( SlotSpin* spin )
{
	// If we are in the first paid spin (NOT a bonus spin)...
	if ( getGameModeId() == MODE_NORMAL ) 
	{
		// then try for the Random Progressive Jackpot
		// If we hit the jackpot...
		// In an all pays slot, numlines = 1
		if ( getRandomProgressiveJackpotResult( 1 ) ||
			m_bForceRandomJackpot)
		{
			// then add a jackpot scatter to the spin.
			spin->addScatter( 0,
				getPayout( EVT_RANDOM_PROGRESSIVE_WINNER )->payoutAmount, 1,
				true, EVT_RANDOM_PROGRESSIVE_WINNER, 0 );
		}
    }

	// In third party games (API) we include the jackpot in every spin so that they can update it.
	// If no jackpot, they have not won the jackpot
	setJackpotAmount( getProgressiveValue() );
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 ReelBloodGuts::getScatterWinResult()   
///
/// @param   spin				The current spin object.
/// @param   testIcon			The scatter icon that we are searching for.
/// @param   scatterPositions	The bit mask for the scatters positions that we find.
///
/// @return	 SpinResult			A payout from the SpinResult enumeration.
///
/// This helper method is used to evaluate the spin for scatters.
////////////////////////////////////////////////////////////////////////////
ReelBloodGuts::SpinResult ReelBloodGuts::getScatterWinResult( SlotSpin* spin, uint8 testIcon, int32& scatterPositions ) const
{
	// Declare a local variable to keep up with the icon count.
	uint8 numScatterIcons = 0;

	// Initialize the scatterPositions.
	scatterPositions = 0;

	// Loop through each reel.
  	for( int reel = 0; reel < getReelCount(); reel++ )
	{
		// The position bit for the current icon in case it needs to be stored in a mask.
		uint8 position = reel;

		// Loop through each row in the wheel house.
		for( int row = 0; row < 3; row++ )
		{
			// Get the current stop.
			uint8 stop = relativeWheelPosition( getGameModeId(), reel,
				spin->getStops()[ reel ], row );

			// Get the icon for the current stop.
			uint8 currentIcon = getIcon( getGameModeId(), reel, stop )->iconid;
			
			// If the icon is the one we are looking for...
			if ( currentIcon == testIcon )
			{
				// then increase the icon count
				numScatterIcons++;

				// Set the bit in the position mask for this stop.
				scatterPositions |= 1 << position;
			}

			// Move to the next bit in the position mask.
            position += 5;
		}
	}

	// Use the array to look up the spin result for numScatterIcons.
	return lookupScatterWinSpinResult( testIcon, numScatterIcons );
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 ReelBloodGuts::evaluateScatterWin()
///
/// This function determines if the current spin wins a scatter payment and
/// adds the scatter.
////////////////////////////////////////////////////////////////////////////
void ReelBloodGuts::evaluateScatterWin( SlotSpin* spin, SpinResult srScatterResult, int32 scatterPositions )
{
	// If we got the scatter win (or almost got it)
	if ( srScatterResult != EVT_LOSER )
	{
		// Initialize our multiplier for scatters.
		int32 multiplier = 1;
		
		// Get the base payout for the scatter.
		int32 nPayout = getPayout( srScatterResult )->payoutAmount;

		// Add the payout (with multiplier and bet per line) to the total win for this spin. getBetPerLine()=total bet (because it is an all pays slot)
		increaseTotalWin( nPayout * multiplier * (getBetPerLine()/minimumBetRB) );
    
		// If we have more than three scatters, BONUS awarded
		int nFreeSpinsAwarded = 0;
		if (srScatterResult == EVT_THREE_SCATTER ||  srScatterResult == EVT_FOUR_SCATTER || srScatterResult == EVT_FIVE_SCATTER)
		{
		//	nFreeSpinsAwarded = 15;
		}

		// Add a scatter to the spin.
		spin->addScatter( scatterPositions, nPayout * (getBetPerLine()/minimumBetRB), multiplier, false, srScatterResult, nFreeSpinsAwarded );
	}
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 ReelBloodGuts::classifyScatters( SlotSpin* spin )
///
/// @param   spin       Pointer to the spin with stop data.
///
/// This is an override of the framework method.  
/// It determines if the current spin merits free spins or scatter payments.
////////////////////////////////////////////////////////////////////////////
void ReelBloodGuts::classifyScatters( SlotSpin* spin )
{	
	// Try for the Random Progressive Jackpot
	tryForJackpot( spin );

	// Declare a local variable to track the scatter positions.
	int32 nScatterPositions = 0;

	// See if there is a scatter.
	SpinResult srResult = getScatterWinResult( spin, ICON_SCATTER, nScatterPositions );

	// Add the scatter, if any.
	evaluateScatterWin( spin, srResult, nScatterPositions );

	//  If three or more scatters, activate bonus game
	if (srResult == EVT_THREE_SCATTER ||  srResult == EVT_FOUR_SCATTER || srResult == EVT_FIVE_SCATTER)
	{
		initializeSubGame(pbgFirstBar);
		m_subGame->update( SGIDX_BONUS_GAME_STATE, pbgFirstBar );  
	}
	else
	{
		m_subGame->update( SGIDX_BONUS_GAME_STATE, pbgNone );  
	}
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 ReelBloodGuts::classifyAllPays( SlotSpin* spin )
///
/// @param   spin       Pointer to the spin with stop data.
///
/// This is an override of the framework method.  
/// It determines if the current spin merits free spins or scatter payments.
////////////////////////////////////////////////////////////////////////////
void ReelBloodGuts::classifyAllPays( SlotSpin* spin )
{	
	ALL_PAYS_PAYOUT pRow[3];
	uint8 stop, icon;

	// Loop through each first row icon.
	for ( int firstRow = 0; firstRow < 3; firstRow++ )
	{
		stop = relativeWheelPosition( getGameModeId(), 0, spin->getStops()[0], firstRow );
		pRow[firstRow].Icon = getIcon( getGameModeId(), 0, stop )->iconid;
		pRow[firstRow].Positions =  1 << (firstRow*5);
		pRow[firstRow].WinType = EVT_LOSER;
		pRow[firstRow].Multiplier = 1;	
		
		bool8 bContinueReels = true;

  		// Loop through each reel
		for( int reel = 1; reel < getReelCount() && bContinueReels; reel++ )
		{
			// The position bit for the current icon in case it needs to be stored in a mask.
			uint8 position = reel;
			uint8 nMatches = 0;

			for (int row = 0; row < 3; row++ )
			{	
				// Get current icon
				stop = relativeWheelPosition( getGameModeId(), reel, spin->getStops()[reel], row );
				icon = getIcon( getGameModeId(), reel, stop )->iconid;
		
				// We are not looking for scatters here (they are calculated in classifyScatters function)
				if (pRow[firstRow].Icon != ICON_SCATTER)
				{
					if (pRow[firstRow].Icon == ICON_WILD)
					{
						pRow[firstRow].Icon = icon;
						pRow[firstRow].Positions |= 1 << position;
						nMatches++;
					}
					else if (pRow[firstRow].Icon == icon)
					{
						pRow[firstRow].Positions |= 1 << position;
						nMatches++;
					}
					else if (icon == ICON_WILD)
					{
						pRow[firstRow].Positions |= 1 << position;
						nMatches++;
					}

					// Move to the next bit in the position mask
					position += 5;
				}
			}

			if (nMatches == 0)
			{
				// If we did not find the symbol in this reel, stop looking for it
				bContinueReels = false;
			}
			else
			{
				// If we found the symbol in this reel, continue looking for it
				pRow[firstRow].WinType = lookupScatterWinSpinResult( pRow[firstRow].Icon, reel+1 );
				// If the symbol was found twice on this reel (it happens if there is a wild and a symbol), we increment the multiplier
				if (nMatches > 1)
					pRow[firstRow].Multiplier = pRow[firstRow].Multiplier * nMatches;
			}
		}
	}

	// Add the payouts, if any
	for ( int firstRow = 0; firstRow < 3; firstRow++ )
	{
		if (pRow[firstRow].WinType != EVT_LOSER)
		{
			// Add payout as a scatter
			int32 nPayout = getPayout( pRow[firstRow].WinType )->payoutAmount;

			increaseTotalWin( nPayout * pRow[firstRow].Multiplier * (getBetPerLine()/minimumBetRB) );

			spin->addScatter( pRow[firstRow].Positions,
				nPayout * (getBetPerLine()/minimumBetRB), pRow[firstRow].Multiplier, false, pRow[firstRow].WinType, 0 );
		}
	}
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 ReelBloodGuts::isValidGuts()   
///
/// @return	 bool8      True if gut is valid.    
///
/// This helper method is used to see if the current gut ID is valid.
////////////////////////////////////////////////////////////////////////////
bool8 ReelBloodGuts::isValidGuts()
{
	// Make sure that the m_aScatterWinSpinResults array has an entry for each reel,
	// and an additional entry in case there are 0 icons on the line.
	ASSERT( countof( m_aScatterWinSpinResults[0] ) == getReelCount( MODE_NORMAL ) + 1 );

	// Now make sure that both game modes have the same number of reels.
	//ASSERT( getReelCount( MODE_NORMAL ) == getReelCount( MODE_FREE_SPIN ) );

	// Finally just check to see if the gut is within the right range. GENESIS GAMES: [9000,9500)
    return ( getGutsId() >= 9000 && getGutsId() < 9500 );
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 ReelBloodGuts::getGameModeMultiplierAndNoteId( uint8& nGameModeMultiplier, uint8& nNoteId )
///
/// @param   nGameModeMultiplier	The multiplier for the game mode.
/// @param   nNoteId				The note ID for the game mode.
///
/// This helper method is used to get the game mode multiplier and note ID.
////////////////////////////////////////////////////////////////////////////
void ReelBloodGuts::getGameModeMultiplierAndNoteId( uint8& nGameModeMultiplier,
	uint8& nNoteId )
{
	// If we are in free spin mode...
//    if ( getGameModeId() == MODE_FREE_SPIN )
//    {
//		
//    }
//	// Otherwise...
//    else
//    {
		// just defer to the base class.
        SlotGuts::getGameModeMultiplierAndNoteId( nGameModeMultiplier, nNoteId );
    //}
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 ReelBloodGuts::preSpinClassifyResults( SlotSpin* spin )
///
/// @param   spin       Pointer to the spin with stop data.
///
/// This method is overridden when running in debug spins mode to alter the reel stops.
////////////////////////////////////////////////////////////////////////////
void ReelBloodGuts::preSpinClassifyResults( SlotSpin* spin )
{
	// Hide bottle contents
	m_subGame->setServerOnly(SGDIDX_BOTTLE1_CONTENT, true);
	m_subGame->setServerOnly(SGDIDX_BOTTLE2_CONTENT, true);
	m_subGame->setServerOnly(SGDIDX_BOTTLE3_CONTENT, true);
	m_subGame->setServerOnly(SGDIDX_BOTTLE4_CONTENT, true);
	m_subGame->setServerOnly(SGDIDX_BOTTLE5_CONTENT, true);
	m_subGame->setServerOnly(SGDIDX_BOTTLE6_CONTENT, true);
	m_subGame->setServerOnly(SGDIDX_BOTTLE7_CONTENT, true);
	m_subGame->setServerOnly(SGDIDX_BOTTLE8_CONTENT, true);
	m_subGame->setServerOnly(SGDIDX_BOTTLE9_CONTENT, true);
	m_subGame->setServerOnly(SGDIDX_BOTTLE10_CONTENT, true);
	m_subGame->setServerOnly(SGDIDX_BOTTLE11_CONTENT, true);
	m_subGame->setServerOnly(SGDIDX_BOTTLE12_CONTENT, true);
	m_subGame->setServerOnly(SGDIDX_BOTTLE13_CONTENT, true);
	m_subGame->setServerOnly(SGDIDX_BOTTLE14_CONTENT, true);
	m_subGame->setServerOnly(SGDIDX_BOTTLE15_CONTENT, true);
	m_subGame->setServerOnly(SGDIDX_PAYALL_AMOUNT, true);

	SlotGuts::preSpinClassifyResults(spin);
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 ReelBloodGuts::postSpinClassifyResults( SlotSpin* spin )
///
/// @param   spin       Pointer to the spin with stop data.
///
/// This method is overridden to see if we can complete the game.
////////////////////////////////////////////////////////////////////////////
void ReelBloodGuts::postSpinClassifyResults( SlotSpin* spin )
{
	// If we are in a bonus game, the spin cannot complete
	if ( m_subGame->getData(SGIDX_BONUS_GAME_STATE) != pbgNone )
	{
		setGameCanComplete( false );
	}
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 ReelBloodGuts::processClientData()
///
/// @param   bonusGameType      The current type of bonus game.
/// @param   bonusDataCount     The number of integers passed from the client.
/// @param   bonusData			An array of integers passed from the client.
/// 
/// This is an override of the framework method.
/// 
////////////////////////////////////////////////////////////////////////////
bool8
ReelBloodGuts::processClientData( uint8 bonusGameType, int32 bonusDataCount, int32 *bonusData )
{
	// Start by assuming that we do not need to reply to this message
	bool8 bReply = false;

	// The message we are expecting from the client is a bottle position in the bar
	if ( m_subGame->getData(SGIDX_BONUS_GAME_STATE) == pbgFirstBar || m_subGame->getData(SGIDX_BONUS_GAME_STATE) == pbgSecondBar )
	{
		if (bonusDataCount == 1 && bonusData != NULL)
		{
			int32 nBottle = *bonusData;
			if (nBottle >= 1 && nBottle <= 15)
			{
				// If we have just moved to bar2, delete bottle contents and generate new ones
				if (m_subGame->getData(SGDIDX_MOVING_TO_BAR2) == 1)
				{
					generateSubGamePayouts(5);
				}

				// Was this bottle chosen before?
				int nSubGameID = SGDIDX_END_BASE_SUB_GAME_DATA_RESERVED_RANGE + (nBottle-1);
				if (m_subGame->isServerOnly(nSubGameID))
				{
					int32 nValue = m_subGame->getData(nSubGameID);
					if (nValue == 0)
					{
						// Collect bottle: bonus game ends
						endBonusGame();
					}
					else if (nValue == -1)
					{
						nValue = m_subGame->getData(SGDIDX_PAYALL_AMOUNT);
						addReelBloodBonusGame(m_subGame->getData(SGIDX_BONUS_GAME_STATE) - 1, getFeatureGamePickIndex(), nValue, 1, nValue, 0);
						
						// Pay all bottle: go to bar2 (we just have this one if we are in bar1)
						continueBonusGame();
					}
					else
					{
						addReelBloodBonusGame(m_subGame->getData(SGIDX_BONUS_GAME_STATE) - 1, getFeatureGamePickIndex(), nValue, 1, nValue, 0);

						// If this bottle was not chosen before we send it to the client
						m_subGame->setServerOnly(nSubGameID, false);
					}

					incrementFeatureGamePickIndex();
					
					bReply = true;
				}
			}
		}
	}

	return bReply;
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 ReelBloodGuts::hasSavedGames()
///
/// @param   bool8   True/False indicating if this machine supports saved games.
///
/// This is a framework override to determine if this machine supports saved games.
////////////////////////////////////////////////////////////////////////////
bool8 ReelBloodGuts::hasSavedGames()
{
	// We support saved games so always return true.
	return true;
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 ReelBloodGuts::Debug_SetLoserStops( uint8* pStops )
///
/// @param   pStops		The stops to set.
///
/// Base class override, returns a losing spin triggered by the debug tool
////////////////////////////////////////////////////////////////////////////
void ReelBloodGuts::Debug_SetLoserStops( uint8* pStops )
{
	pStops[0]=1;
	pStops[1]=11;
	pStops[2]=22;
	pStops[3]=31;
	pStops[4]=41;
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 ReelBloodGuts::getProgressiveValue( )
///
/// Returns current jackpot value for HydroHeat guts
////////////////////////////////////////////////////////////////////////////
int32 ReelBloodGuts::getProgressiveValue()
{
   SlotsSQL *slotTracker = (SlotsSQL *) tracker;
   int32 jackpot = 0;

    if ( tracker )
    {
    	slotTracker->lock();
		slotTracker->getJackpot(getJackpotId(), jackpot, getInfo()->forMoney, 1);
    	slotTracker->unlock();
    }

	return jackpot;
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 ReelBloodGuts::initializeSubGame( )
///
/// Initialize subgame data
////////////////////////////////////////////////////////////////////////////
void ReelBloodGuts::initializeSubGame ( uint8 Bar )
{
	// Choose a BAR1 pool
	uint8 poolBar1 = getRandomValueFromProbabilityTable( PT_BAR1_POOLS );
	m_subGame->setServerOnly(SGDIDX_BAR1_POOL, true);
	m_subGame->update(SGDIDX_BAR1_POOL, poolBar1);

	m_subGame->setServerOnly(SGDIDX_MOVING_TO_BAR2, false);
	m_subGame->update(SGDIDX_MOVING_TO_BAR2, 0);

	initializeFeatureGamePickIndex();
	
	generateSubGamePayouts(poolBar1);

}

void ReelBloodGuts::endBonusGame()
{
	// If the bonus game has ended, we have to send all bottle contents
	showAllBottleContents();
	
	// Bonus game has ended
	m_subGame->update(SGIDX_BONUS_GAME_STATE, pbgNone);
	setGameCanComplete( true );
}

void ReelBloodGuts::continueBonusGame()
{
	// If the bonus game has ended, we have to send all bottle contents
	showAllBottleContents();
	
	// We move to second bar
	m_subGame->update(SGIDX_BONUS_GAME_STATE, pbgSecondBar);
	m_subGame->update(SGDIDX_MOVING_TO_BAR2, 1);
}

void ReelBloodGuts::generateSubGamePayouts(int8 Pool)
{
	// Initialize bottle contents
	m_subGame->setServerOnly(SGDIDX_BOTTLE1_CONTENT, true);
	m_subGame->setServerOnly(SGDIDX_BOTTLE2_CONTENT, true);
	m_subGame->setServerOnly(SGDIDX_BOTTLE3_CONTENT, true);
	m_subGame->setServerOnly(SGDIDX_BOTTLE4_CONTENT, true);
	m_subGame->setServerOnly(SGDIDX_BOTTLE5_CONTENT, true);
	m_subGame->setServerOnly(SGDIDX_BOTTLE6_CONTENT, true);
	m_subGame->setServerOnly(SGDIDX_BOTTLE7_CONTENT, true);
	m_subGame->setServerOnly(SGDIDX_BOTTLE8_CONTENT, true);
	m_subGame->setServerOnly(SGDIDX_BOTTLE9_CONTENT, true);
	m_subGame->setServerOnly(SGDIDX_BOTTLE10_CONTENT, true);
	m_subGame->setServerOnly(SGDIDX_BOTTLE11_CONTENT, true);
	m_subGame->setServerOnly(SGDIDX_BOTTLE12_CONTENT, true);
	m_subGame->setServerOnly(SGDIDX_BOTTLE13_CONTENT, true);
	m_subGame->setServerOnly(SGDIDX_BOTTLE14_CONTENT, true);
	m_subGame->setServerOnly(SGDIDX_BOTTLE15_CONTENT, true);
	m_subGame->setServerOnly(SGDIDX_PAYALL_AMOUNT, true);

	// Once we have chosen a pool, we have to disorder it and store it in the subgame data
	int32 g_AuxPoolPayouts[15];
	int32 iAllPaysAmount = 0;
	// First we copy the chosen pool
	for(int i=0; i<15; i++)
	{
		// We have to multiply according to the user bet, just in case it is not a pay all(-1) or a collect(0)
		if ((g_poolPayouts[Pool-1][i] == -1) || (g_poolPayouts[Pool-1][i] == 0))
		{
			g_AuxPoolPayouts[i] = g_poolPayouts[Pool-1][i];
		}
		else
		{
			g_AuxPoolPayouts[i] = g_poolPayouts[Pool-1][i] * (getBetPerLine()/minimumBetRB);
			iAllPaysAmount += g_AuxPoolPayouts[i];
		}
	}
	// Then we disorder it
	for(int i=0; i<15; i++)
	{
		int32 iAuxPayout = g_AuxPoolPayouts[i];
		
		// Choose a value to swap with
		uint8 iRandomPosition = getrand() % 15;
		
		// Swap the values
		g_AuxPoolPayouts[i] = g_AuxPoolPayouts[iRandomPosition];
		g_AuxPoolPayouts[iRandomPosition] = iAuxPayout;
	}
	// Once we have disordered the values, store them in the subgame data
	m_subGame->update(SGDIDX_BOTTLE1_CONTENT, g_AuxPoolPayouts[0]);
	m_subGame->update(SGDIDX_BOTTLE2_CONTENT, g_AuxPoolPayouts[1]);
	m_subGame->update(SGDIDX_BOTTLE3_CONTENT, g_AuxPoolPayouts[2]);
	m_subGame->update(SGDIDX_BOTTLE4_CONTENT, g_AuxPoolPayouts[3]);
	m_subGame->update(SGDIDX_BOTTLE5_CONTENT, g_AuxPoolPayouts[4]);
	m_subGame->update(SGDIDX_BOTTLE6_CONTENT, g_AuxPoolPayouts[5]);
	m_subGame->update(SGDIDX_BOTTLE7_CONTENT, g_AuxPoolPayouts[6]);
	m_subGame->update(SGDIDX_BOTTLE8_CONTENT, g_AuxPoolPayouts[7]);
	m_subGame->update(SGDIDX_BOTTLE9_CONTENT, g_AuxPoolPayouts[8]);
	m_subGame->update(SGDIDX_BOTTLE10_CONTENT, g_AuxPoolPayouts[9]);
	m_subGame->update(SGDIDX_BOTTLE11_CONTENT, g_AuxPoolPayouts[10]);
	m_subGame->update(SGDIDX_BOTTLE12_CONTENT, g_AuxPoolPayouts[11]);
	m_subGame->update(SGDIDX_BOTTLE13_CONTENT, g_AuxPoolPayouts[12]);
	m_subGame->update(SGDIDX_BOTTLE14_CONTENT, g_AuxPoolPayouts[13]);
	m_subGame->update(SGDIDX_BOTTLE15_CONTENT, g_AuxPoolPayouts[14]);
	m_subGame->update(SGDIDX_PAYALL_AMOUNT, iAllPaysAmount);

	m_subGame->update(SGDIDX_MOVING_TO_BAR2, 0);
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 void ReelBloodGuts::initializeFeatureGamePickIndex()
///
/// This helper method is used to initialize the subgame data that tracks
/// which bottle pick is currently being processed.
////////////////////////////////////////////////////////////////////////////
void ReelBloodGuts::initializeFeatureGamePickIndex()
{
	// Initialize the subgame data to the 0th pick
	m_subGame->setServerOnly(SGDIDX_CURRENT_PICK_INDEX, true);
    m_subGame->update( SGDIDX_CURRENT_PICK_INDEX, 0 );
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 void ReelBloodGuts::incrementFeatureGamePickIndex()
///
/// This helper method is used to increment the subgame data that tracks
/// which bottle pick is currently being processed.
////////////////////////////////////////////////////////////////////////////
void ReelBloodGuts::incrementFeatureGamePickIndex()
{
	// Get the current value and then add 1.
    m_subGame->update( SGDIDX_CURRENT_PICK_INDEX, getFeatureGamePickIndex() + 1 );
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 int32 ReelBloodGuts::getFeatureGamePickIndex()
///
/// This helper method is used to get the subgame data that tracks how many
/// bottle picks have been made.
////////////////////////////////////////////////////////////////////////////
int32 ReelBloodGuts::getFeatureGamePickIndex() const
{
	// Return the subgame data.
	return m_subGame->getData( SGDIDX_CURRENT_PICK_INDEX );
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 int32 ReelBloodGuts::addReelBloodBonusGame 
///
/// This method is used to store a bonus game in the database. Similar to
/// the addBonusGame in slotsguts.cpp but this is a special one for all pays
/// slots, as we don't have to divide by bet per line
////////////////////////////////////////////////////////////////////////////
BonusGame*
ReelBloodGuts::addReelBloodBonusGame(uint8 bonusgametype,uint8 bonusgameindex,int32 basepayoutamount,int32 totalmultiplier,int32 totalbonuspayoutamount,int32 freespins,int64 overridegamenum,bool8 bSaved)
{
	int32 nPayout = totalbonuspayoutamount;

	if ( getBetPerLine() > 0 )
	{
		increaseTotalWin(nPayout);
	}

	SlotSpin* spin = getSlotSpin();
	// Include bonus game 
	return spin->addBonusGame(bonusgametype, bonusgameindex, basepayoutamount, totalmultiplier, totalbonuspayoutamount, freespins, overridegamenum);

}

void ReelBloodGuts::showAllBottleContents()
{
	// If the bonus game has ended, we have to send all bottle contents
	m_subGame->setServerOnly(SGDIDX_BOTTLE1_CONTENT, false);
	m_subGame->setServerOnly(SGDIDX_BOTTLE2_CONTENT, false);
	m_subGame->setServerOnly(SGDIDX_BOTTLE3_CONTENT, false);
	m_subGame->setServerOnly(SGDIDX_BOTTLE4_CONTENT, false);
	m_subGame->setServerOnly(SGDIDX_BOTTLE5_CONTENT, false);
	m_subGame->setServerOnly(SGDIDX_BOTTLE6_CONTENT, false);
	m_subGame->setServerOnly(SGDIDX_BOTTLE7_CONTENT, false);
	m_subGame->setServerOnly(SGDIDX_BOTTLE8_CONTENT, false);
	m_subGame->setServerOnly(SGDIDX_BOTTLE9_CONTENT, false);
	m_subGame->setServerOnly(SGDIDX_BOTTLE10_CONTENT, false);
	m_subGame->setServerOnly(SGDIDX_BOTTLE11_CONTENT, false);
	m_subGame->setServerOnly(SGDIDX_BOTTLE12_CONTENT, false);
	m_subGame->setServerOnly(SGDIDX_BOTTLE13_CONTENT, false);
	m_subGame->setServerOnly(SGDIDX_BOTTLE14_CONTENT, false);
	m_subGame->setServerOnly(SGDIDX_BOTTLE15_CONTENT, false);

	// Modify pay all values (-1 value) with the correct amount
	for (uint8 id = SGDIDX_BOTTLE1_CONTENT; id <= SGDIDX_BOTTLE15_CONTENT; id++)
	{
		if (m_subGame->getData(id) == -1)
		{
			m_subGame->update(id, m_subGame->getData(SGDIDX_PAYALL_AMOUNT));
		}
	}
}